/***************************************************************************//**
 * @file DataTypeSkyDomeLight.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef DATA_TYPE_SKY_DOME_SUN_CPP
#define DATA_TYPE_SKY_DOME_SUN_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <CGI/VisitorFindNode.h>
#include <CGI/DataTypeSkyDomeLight.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

DataTypeSkyDomeLight::DataTypeSkyDomeLight( osg::Node *node ) :
    m_elevation ( 0.0 )
{
    VisitorFindNode findPAT( "SkyDomeLightPAT" );
    VisitorFindNode findLight( "SkyDomeLightSource" );

    node->accept( findPAT );
    node->accept( findLight );

    m_skyDomeLightPAT = dynamic_cast<osg::PositionAttitudeTransform *>( findPAT.getFirstNode().get() );

    m_lightSource = dynamic_cast<osg::LightSource *>( findLight.getFirstNode().get() );
}

////////////////////////////////////////////////////////////////////////////////

void DataTypeSkyDomeLight::update()
{
    updatePosition();
    updateLighting();
}

////////////////////////////////////////////////////////////////////////////////

osg::Vec4 DataTypeSkyDomeLight::getAmbientColor()
{
    double elevation_deg = osg::RadiansToDegrees( m_elevation );

    if ( elevation_deg < 0.0 )
    {
        if ( elevation_deg < -6.0 )
        {
            return osg::Vec4( 0.0, 0.0, 0.0, 0.0 );
        }
        else
        {
            double coef = ( elevation_deg + 6.0 ) / 12.0;

            return osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) * coef;
        }
    }

    return osg::Vec4( 1.0, 1.0, 1.0, 1.0 );
}

////////////////////////////////////////////////////////////////////////////////

osg::Vec4 DataTypeSkyDomeLight::getDiffuseColor()
{
    double elevation_deg = osg::RadiansToDegrees( m_elevation );

    if ( elevation_deg < 0.0 )
    {
        if ( elevation_deg < -0.27 )
        {
            return osg::Vec4( 0.0, 0.0, 0.0, 0.0 );
        }
        else
        {
            double coef = ( elevation_deg + 0.27 ) / 0.27;

            return osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) * coef;
        }
    }

    return osg::Vec4( 1.0, 1.0, 1.0, 1.0 );
}

////////////////////////////////////////////////////////////////////////////////

void DataTypeSkyDomeLight::updatePosition()
{
    if ( m_skyDomeLightPAT.valid() )
    {
        osg::Quat elevation;

        elevation.makeRotate( 0.0         , osg::Vec3d( 1.0, 0.0, 0.0 ),
                             -m_elevation , osg::Vec3d( 0.0, 1.0, 0.0 ),
                              M_PI / 2.0  , osg::Vec3d( 0.0, 0.0, 1.0 ) );

        m_skyDomeLightPAT->setAttitude( elevation );
    }
    else
    {
        cerr << "ERROR! Wrong node pointer." << endl;
    }
}

////////////////////////////////////////////////////////////////////////////////

void DataTypeSkyDomeLight::updateLighting()
{
    if ( m_lightSource.valid() )
    {
        m_ambientColor = getAmbientColor();
        m_diffuseColor = getDiffuseColor();

        if ( m_ambientColor_prev != m_ambientColor || m_diffuseColor_prev != m_diffuseColor )
        {
            m_lightSource->getLight()->setAmbient( m_ambientColor );
            m_lightSource->getLight()->setDiffuse( m_diffuseColor );
        }
    }
    else
    {
        cerr << "ERROR! Wrong node pointer." << endl;
    }

    m_ambientColor_prev = m_ambientColor;
    m_diffuseColor_prev = m_diffuseColor;
}
